package com.angel.web.app;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.base.JsonEntity;
import com.angel.core.util.JsonUtil;
import com.angel.health.BuzCustomer;
import com.angel.health.BuzCustomerFriend;
import com.angel.provider.health.BuzCustomerFriendProvider;
import com.angel.provider.health.BuzCustomerProvider;
import com.github.pagehelper.StringUtil;

/** 
* @author 作者姓名  cheng
* @version 
* 创建时间：2016年9月30日 上午11:31:11 
* 类说明  app用户关系操作
*/
@Controller
@RequestMapping("app/acfc")
public class AppCustomerFriendController {
	
	@Autowired
	private BuzCustomerFriendProvider bcfProvider;
	
	@Autowired
	private BuzCustomerProvider bcProvider;

	@ResponseBody
	@RequestMapping(value="index",produces="application/json; charset=UTF-8")
	public String index(BuzCustomerFriend bcf){
		bcf.setEnable(1);
		List<Map<String, Object>> list = bcfProvider.findFriendAll(bcf);
		return JsonUtil.toJson(new JsonEntity("200",list));
	}
	
	/**
	 * 新增关系
	 * 查看用户是否存在
	 * 若存在则添加
	 * @param bcf
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "addFriend",produces="application/json; charset=UTF-8")
	public String addFriend(BuzCustomerFriend bcf,BuzCustomer bc){
		bcf.setEnable(1);
		if(StringUtil.isEmpty(bc.getMemPhone()) || bcf.getCustomerId() == null){
			return JsonUtil.toJson(new JsonEntity("-1","参数不正确！"));
		}
		List<Map<String, Object>> bcList = bcProvider.findCustomerByPhone(bc);
		String friendId = "";
		if(bcList.size() > 0){
			friendId = bcList.get(0).get("id").toString();
			bcf.setFriendCustomerId(Integer.parseInt(friendId));
		}else{
			return JsonUtil.toJson(new JsonEntity("-1","用户不存在！"));
		}
		List<Map<String, Object>> list = bcfProvider.findFriendAll(bcf);
		if(list.size() > 0){
			return JsonUtil.toJson(new JsonEntity("-1","已添加！"));
		}
		int insert = bcfProvider.insert(bcf);
		if(insert > 0){
			return JsonUtil.toJson(new JsonEntity("200",list.get(0)));
		}else{
			return JsonUtil.toJson(new JsonEntity("-1","添加失败！"));
		}
	}
	
	/**
	 * 新增关系
	 * @param bcf
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "del",produces="application/json; charset=UTF-8")
	public String del(BuzCustomerFriend bcf){
		if(bcf.getId() == null){
			return JsonUtil.toJson(new JsonEntity("-1","参数不正确！"));
		}
		int delById = bcfProvider.delById(bcf);
		if(delById > 0){
			return JsonUtil.toJson(new JsonEntity("200","删除成功！"));
		}else{
			return JsonUtil.toJson(new JsonEntity("-1","删除失败！"));
		}
	}
}
